package com.yungu.swift.system.sys.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.system.sys.dao.SysPlatformMapper;
import com.yungu.swift.system.sys.model.dto.SysPlatformDto;
import com.yungu.swift.system.sys.service.SysPlatformService;
import com.yungu.swift.utils.JsonUtils;
import com.yungu.swift.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import static com.yungu.swift.system.config.NacosListener.SYS_CONFIG;


/**
 * @author cuixiuyin 平台信息管理
 **/
@Service
@Slf4j
public class SysPlatformServiceImpl extends BaseServiceImpl<SysPlatformDto> implements SysPlatformService {
    @Autowired
    private SysPlatformMapper sysPlatformInfoMapper;

    @Override
    public IMapper<SysPlatformDto> getMapper() {
        return sysPlatformInfoMapper;
    }

    @Override
    public ResponseData<SysPlatformDto> get(Map<String, Object> paramMap) {
        List<SysPlatformDto> list = sysPlatformInfoMapper.list(paramMap);
        if (CollectionUtils.isNotEmpty(list)) {
            SysPlatformDto sysPlatformDto = list.get(0);
            // 补全图片信息
            String platformInfo = sysPlatformDto.getPlatformInfo();
            if (StringUtils.isNotEmpty(platformInfo)) {
                try {
                    SysPlatformDto.PlatformInfoDto platformInfoDto = JsonUtils.readValue(platformInfo, SysPlatformDto
                            .PlatformInfoDto.class);
                    platformInfoDto.setLegalJpgPhoto(SYS_CONFIG.getWebSite() + platformInfoDto.getLegalJpgPhoto());
                    sysPlatformDto.setPlatformInfo(JsonUtils.writeJsonStr(platformInfoDto));
                } catch (IOException e) {
                    log.error("SysPlatformService get JsonUtils call error.platformInfo:{}", platformInfo, e);
                }
            }
            // 查询营运规模
            SysPlatformDto.PlatformStatDto platformStatDto = sysPlatformInfoMapper.scale(paramMap);
            sysPlatformDto.setPlatformStat(JsonUtils.toJSONString(platformStatDto));
            return ResponseData.buildSuccessResponse(sysPlatformDto);
        }
        return ResponseData.buildSuccessResponse(new SysPlatformDto());
    }
}